/**
 * Created by shc@lierda.com on 2017/2/22.
 */
var React = require('react');

var ajax = require('../tools/ajax');
var IncreaseId = require('../../web/tools/IncreaseId');
var AttrContent = require('../widget/AttrContent');
var MsgBox = require('../components/MsgBox');

module.exports = React.createClass({
    getInitialState: function (){
        return {
            list_info:[],
            remainingTime:"",
            rlist:[],
            timeList:[],
            bmFlag:[],
            leftTime:"",
            flag:true,
            word:"库存不足"
        }
    },
    timer:null,
    componentWillUnmount:function(){
        if(this.timer){
            clearInterval(this.timer);
            this.timer = null;
        }

    },

    componentDidMount: function () {
        if(this.props.list_info){
            this.setState({list_info:this.props.list_info})
        }
        if(this.props.leftTime){
            this.setState({leftTime:this.props.leftTime},()=>{
                this.leftTimer(this.state.leftTime);
            })
        }
    },
    /**秒数转化为时间 计时-1秒*/
    leftTimer(leftTime){
        var p=this;
        if(leftTime) {
            this.timer = setInterval(function () {
                leftTime -= 1;
                var days = parseInt(leftTime / 3600 / 24, 10); //计算剩余的天数
                var hours = parseInt(leftTime / 3600 % 24, 10); //计算剩余的小时
                var minutes = parseInt(leftTime / 60 % 60, 10);//计算剩余的分钟
                var seconds = parseInt(leftTime % 60, 10);//计算剩余的秒数
                hours = p.checkTime(hours);
                minutes = p.checkTime(minutes);
                seconds = p.checkTime(seconds);
                var remainingTime = days + "天" + hours + "小时" + minutes + "分" + seconds + "秒";
                p.setState({remainingTime: remainingTime});
                if (leftTime == 0) {
                    clearInterval(timer);
                }
            }, 1000);
        }else{
            var remainingTime = "0天0小时0分0秒";
            p.setState({remainingTime: remainingTime});
            p.UpdateIs_discount(p.state.list_info.id);
        }
    },
    /**将0-9的数字前面加上0，例1变为01*/
    checkTime(i){
        if(i<10)
        {
            i = "0" + i;
        }
        return i;
    },
    /**修改Is_discount状态*/
    UpdateIs_discount:function(good_id){
        var p = this;
        ajax("POST","UpdateIs_discount",{good_id:good_id},function(dt){
            if(dt && dt.success){

            }
        });
    },
    /**判断购物车是否存在该商品，存在更新商品数量，否则增加到购物车*/
    addCart: function (e){
        var p = this;
        var userId = localStorage.getItem('userId');
        var goods_id = e.currentTarget.getAttribute("data-id");
        var shop_id = localStorage.getItem("shopId");
        var list_info = p.state.list_info;
        if(goods_id){
            ajax("POST","getZhezhouPro", {
                    user_id: userId,
                    goods_id: goods_id,
                    shop_id: shop_id,
                    is_discount: 1
                }, function (dt) {
                    if (dt.data.length > 0) {
                        p.setState({word:'本商品限购一件'},()=>{
                            p.props.MsgBox(p.state.word);
                        });
                    }else{
                        var id = IncreaseId.increaseId();
                        var goods_name = list_info.goods_name;
                        var goods_price = list_info.discount_price;
                        var goods_number = 1;
                        var goods_attr = p.state.attr_name;
                        var goods_type = list_info.goods_type;
                        var good_tax_rate = list_info.good_tax_rate;
                        var good_tax_fee = good_tax_rate / 100 * goods_price;
                        var judgeShop = localStorage.getItem("judgeShop");
                        ajax("POST","getCartlistByGoodsId",{
                            user_id:userId,
                            goods_id:goods_id,
                            shop_id:shop_id,
                            is_discount:1
                        },function(dt){
                            if(dt&&dt.success) {
                                var cartPro = dt.data[0];
                                if(cartPro){
                                    if(cartPro.shop_id == judgeShop){
                                        ajax("POST", "updateCart", {
                                            user_id: userId,
                                            goods_id: goods_id,
                                            goods_number: 0,
                                            good_tax_fee: good_tax_fee
                                        }, function (dt) {
                                            if (dt && dt.success) {
                                                p.setState({word:'该商品购物车已存在'},()=>{
                                                    p.props.MsgBox(p.state.word);
                                                });
                                            }
                                        });
                                    }else {
                                        if (!judgeShop) {
                                            shop_id = "";
                                        }
                                        ajax("POST", "addCart", {
                                            id: id,
                                            user_id: userId,
                                            goods_id: goods_id,
                                            goods_name: goods_name,
                                            goods_price: goods_price,
                                            goods_number: goods_number,
                                            goods_type: goods_type,
                                            shop_id: shop_id,
                                            goods_attr: goods_attr,
                                            good_tax_fee: good_tax_fee,
                                            is_discount:1
                                        }, function (dt) {
                                            if (dt && dt.success) {
                                                var cartNum = localStorage.getItem("cartNum");
                                                localStorage.cartNum = parseInt(cartNum) + 1;
                                                p.setState({word:'添加购物车成功'},()=>{
                                                    p.props.MsgBox(p.state.word);
                                                });
                                                p.props.fleshNum();

                                            }
                                        });
                                    }
                                } else {
                                    if(!judgeShop){
                                        shop_id = "";
                                    }
                                    ajax("POST","addCart",{
                                        id:id,
                                        user_id:userId,
                                        goods_id:goods_id,
                                        goods_name:goods_name,
                                        goods_price:goods_price,
                                        goods_number:goods_number,
                                        goods_type:goods_type,
                                        shop_id:shop_id,
                                        goods_attr:goods_attr,
                                        good_tax_fee:good_tax_fee,
                                        is_discount:1
                                    },function(dt){
                                        if(dt && dt.success){
                                            var cartNum =localStorage.getItem("cartNum");
                                            localStorage.cartNum = parseInt(cartNum)+1;
                                            p.setState({word:'添加购物车成功'},()=>{
                                                p.props.MsgBox(p.state.word);
                                            });
                                            p.props.fleshNum();
                                        }
                                    });
                                }
                            }
                        });
                    }
                }
            )
        }
    },
    render: function () {
        var p = this;
        var list_info = p.state.list_info;
        if(list_info.goods_img){
            var imgUrl = list_info.goods_img.split(",")[0];
        }
        var num = 1 - (new Date(list_info.discount_end_time)- new Date())/(new Date(list_info.discount_end_time)- new Date(list_info.discount_start_time));
        var person_num = ((num * 100).toFixed(0)>=100 ? 100:(num * 100).toFixed(0)) + "%";
        return (
            <div className="pro-info">
                <div className="info-avatar">
                    <img src={imgUrl} />
                </div>
                <div className="info-content">
                    <div className="pro-title">
                        <span>{list_info.goods_name}</span>
                    </div>
                    <div className="pro-des">
                        <span className="c-h">限时价</span>
                        <span className="c-r">￥{list_info.discount_price}</span>
                        <span className="del-price">￥{list_info.price}</span>
                    </div>
                    <div className="pro-progress">
                        <div>
                            <span className="settime" style={{width:person_num}}></span>
                        </div>
                    </div>
                    <div className="pro-time">
                        剩余时间<span>{p.state.remainingTime}</span>
                    </div>
                </div>
                <div  className="add-cart" onClick={p.addCart} data-id={list_info.id}>
                    <span>加入购物车</span>
                </div>
            </div>
        )
    }
});